{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://raw.githubusercontent.com/Qinbf/tf-model-zoo/master/README_IMG/01.jpg)\n",
    "AI MOOC： **www.ai-xlab.com**  \n",
    "如果你也是AI爱好者，可以添加我的微信一起交流：**sdxxqbf**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "异或\n",
    "0^0 = 0\n",
    "0^1 = 1\n",
    "1^0 = 1\n",
    "1^1 = 0\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.56064168]\n",
      " [ 0.60946632]\n",
      " [-0.83588595]]\n"
     ]
    }
   ],
   "source": [
    "#输入数据\n",
    "X = np.array([[1,0,0],\n",
    "              [1,0,1],\n",
    "              [1,1,0],  \n",
    "              [1,1,1]])\n",
    "#标签\n",
    "Y = np.array([[-1],\n",
    "              [1],\n",
    "              [1],\n",
    "              [-1]])\n",
    "\n",
    "#权值初始化，3行1列，取值范围-1到1\n",
    "W = (np.random.random([3,1])-0.5)*2\n",
    "\n",
    "print(W)\n",
    "#学习率设置\n",
    "lr = 0.11\n",
    "#神经网络输出\n",
    "O = 0\n",
    "\n",
    "def update():\n",
    "    global X,Y,W,lr\n",
    "    O = np.sign(np.dot(X,W)) # shape:(3,1)\n",
    "    W_C = lr*(X.T.dot(Y-O))/int(X.shape[0])\n",
    "    W = W + W_C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.50564168]\n",
      " [ 0.55446632]\n",
      " [-0.83588595]]\n",
      "0\n",
      "[[ 0.45064168]\n",
      " [ 0.49946632]\n",
      " [-0.83588595]]\n",
      "1\n",
      "[[ 0.39564168]\n",
      " [ 0.44446632]\n",
      " [-0.83588595]]\n",
      "2\n",
      "[[ 0.34064168]\n",
      " [ 0.38946632]\n",
      " [-0.83588595]]\n",
      "3\n",
      "[[ 0.34064168]\n",
      " [ 0.38946632]\n",
      " [-0.78088595]]\n",
      "4\n",
      "[[ 0.34064168]\n",
      " [ 0.38946632]\n",
      " [-0.72588595]]\n",
      "5\n",
      "[[ 0.28564168]\n",
      " [ 0.33446632]\n",
      " [-0.72588595]]\n",
      "6\n",
      "[[ 0.28564168]\n",
      " [ 0.33446632]\n",
      " [-0.67088595]]\n",
      "7\n",
      "[[ 0.28564168]\n",
      " [ 0.33446632]\n",
      " [-0.61588595]]\n",
      "8\n",
      "[[ 0.23064168]\n",
      " [ 0.27946632]\n",
      " [-0.61588595]]\n",
      "9\n",
      "[[ 0.23064168]\n",
      " [ 0.27946632]\n",
      " [-0.56088595]]\n",
      "10\n",
      "[[ 0.23064168]\n",
      " [ 0.27946632]\n",
      " [-0.50588595]]\n",
      "11\n",
      "[[ 0.17564168]\n",
      " [ 0.22446632]\n",
      " [-0.50588595]]\n",
      "12\n",
      "[[ 0.17564168]\n",
      " [ 0.22446632]\n",
      " [-0.45088595]]\n",
      "13\n",
      "[[ 0.17564168]\n",
      " [ 0.22446632]\n",
      " [-0.39588595]]\n",
      "14\n",
      "[[ 0.12064168]\n",
      " [ 0.16946632]\n",
      " [-0.39588595]]\n",
      "15\n",
      "[[ 0.12064168]\n",
      " [ 0.16946632]\n",
      " [-0.34088595]]\n",
      "16\n",
      "[[ 0.12064168]\n",
      " [ 0.16946632]\n",
      " [-0.28588595]]\n",
      "17\n",
      "[[ 0.06564168]\n",
      " [ 0.11446632]\n",
      " [-0.28588595]]\n",
      "18\n",
      "[[ 0.06564168]\n",
      " [ 0.11446632]\n",
      " [-0.23088595]]\n",
      "19\n",
      "[[ 0.06564168]\n",
      " [ 0.11446632]\n",
      " [-0.17588595]]\n",
      "20\n",
      "[[ 0.01064168]\n",
      " [ 0.05946632]\n",
      " [-0.17588595]]\n",
      "21\n",
      "[[ 0.01064168]\n",
      " [ 0.05946632]\n",
      " [-0.12088595]]\n",
      "22\n",
      "[[ 0.01064168]\n",
      " [ 0.05946632]\n",
      " [-0.06588595]]\n",
      "23\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "24\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "25\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "26\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "27\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "28\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "29\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "30\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "31\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "32\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "33\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "34\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "35\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "36\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "37\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "38\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "39\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "40\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "41\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "42\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "43\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "44\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "45\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "46\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "47\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "48\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "49\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "50\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "51\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "52\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "53\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "54\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "55\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "56\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "57\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "58\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "59\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "60\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "61\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "62\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "63\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "64\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "65\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "66\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "67\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "68\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "69\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "70\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "71\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "72\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "73\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "74\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "75\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "76\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "77\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "78\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "79\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "80\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "81\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "82\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "83\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "84\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "85\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "86\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "87\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "88\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "89\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "90\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "91\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "92\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "93\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "94\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "95\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "96\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "97\n",
      "[[-0.04435832]\n",
      " [ 0.00446632]\n",
      " [-0.06588595]]\n",
      "98\n",
      "[[ 0.06564168]\n",
      " [ 0.05946632]\n",
      " [-0.01088595]]\n",
      "99\n",
      "k= [5.46266944]\n",
      "d= [6.0299475]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAHHlJREFUeJzt3XmUlNW1/vHvxkQMzgMoF0XUONAg\noLbgECdwQGMcEn/GKfFqYjugkeCNw+V6jVdNUMGAEzORSKsRFUFAGVoQkbGZGxHBFpQhDIqgoiD0\n+f2xy4QoQzdd1afqreezFqupoqzatdTH7Xn3e46FEBARkeSoFbsAERFJLwW7iEjCKNhFRBJGwS4i\nkjAKdhGRhFGwi4gkjIJdRCRhFOwiIgmjYBcRSZgfxPjQAw44IDRq1CjGR4uI5Kxp06atDiHU3dHr\nogR7o0aNKC0tjfHRIiI5y8wWV+Z1WooREUkYBbuISMIo2EVEEkbBLiKSMAp2EZGEUbCLiCSMgl1E\nJGEU7CIiNeGTT6B9e1i7NuMfpWAXEcmkEGDgQCgogKeegnHjMv6RCnYRkUxZvhx+/nO4/HI45BCY\nNg1+9rOMf6yCXUQk3UKAfv2gcWN44w145BGYNAmaNauRj4+yV4yISGKVl0NREZSUwOmnQ58+cOSR\nNVqCOnYRkXTYvBm6doVjj4UpU6B7dxgzpsZDHdSxi4hU37vvwm9+48stF1wAPXr4mnok6thFRHbW\nxo3wwANw3HGwYAEMGABDh0YNdVDHLiKyc0pLvUufPRuuuAK6dYN69WJXBahjFxGpmvXr4c47oVUr\nWL0aBg+G55/PmlAHdewiIpX31lvw29/CwoVwww3w6KOw996xq/oedewiIjuybh3cfDOceSZUVPgo\nY69eWRnqoGAXEdm+Yc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M2d5ZqyGEwanXdMT/t664JmvLlMp854SzrTynsa2E\nMrM9gJeB9iGEdbHrybQQwmagReqa4CAzaxpCyMh1lawN9hDC2dv7czO7FrgQaBMSMrO5o++cB5YA\nh2zx+GBgWaRaJIPM7Id4qBeHEF6JXU9NCiF8ZmZj8esqGQn2nFyKMbO2wF3ARSGE9bHrkbSZChxp\nZoeZ2a7AFcCQyDVJmpmZAX2BeSGEx2LXUxPMrO6303tm9iPgbOC9TH1eTgY78CSwJzDKzGaaWY/Y\nBWWamV1qZkuAk4FhZjYidk3plrogfiswAr+g9mIIYW7cqjLPzJ4HJgJHm9kSM/tN7Joy7FTgV0Dr\n1L+/M83sgthFZVh9YIyZzcYbmFEhhKGZ+jDdeSoikjC52rGLiMg2KNhFRBJGwS4ikjAKdhGRhFGw\ni4gkjIJdRCRhFOwiIgmjYBcRSZj/D6vIR5dj+d5WAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x28c8af6fef0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for i in range(100):\n",
    "    update()#更新权值\n",
    "    print(W)#打印当前权值\n",
    "    print(i)#打印迭代次数\n",
    "    O = np.sign(np.dot(X,W))#计算当前输出  \n",
    "    if(O == Y).all(): #如果实际输出等于期望输出，模型收敛，循环结束\n",
    "        print('Finished')\n",
    "        print('epoch:',i)\n",
    "        break\n",
    "\n",
    "#正样本\n",
    "x1 = [0,1]\n",
    "y1 = [1,0]\n",
    "#负样本\n",
    "x2 = [0,1]\n",
    "y2 = [0,1]\n",
    "\n",
    "#计算分界线的斜率以及截距\n",
    "k = -W[1]/W[2]\n",
    "d = -W[0]/W[2]\n",
    "print('k=',k)\n",
    "print('d=',d)\n",
    "\n",
    "xdata = (-2,3)\n",
    "\n",
    "plt.figure()\n",
    "plt.plot(xdata,xdata*k+d,'r')\n",
    "plt.scatter(x1,y1,c='b')\n",
    "plt.scatter(x2,y2,c='y')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
